const db = require("../../config/db")
// let users = await promisePool.query("SELECT * FROM students ");
module.exports = {
    // post请求添加数据到数据库
    addblogItem(contentId, title, contentHtml, createTimer, articleClassVal, CoverImageurl, author, articleTagValue, articledescribe) {

        db.promisePool.query(`insert into db_blog_item(content_id,title, content_html,create_timer,article_class,cove_image,author,article_tag,article_describe) values(?,?,?,?,?,?,?,?,?)`, [contentId, title, contentHtml, createTimer, articleClassVal, CoverImageurl, author, articleTagValue, articledescribe])

    },

    // 前端 请求博客数据
    getblogItem(page) {
        // 请求到得数据一定要return回去
        let sql = `SELECT * FROM db_blog_item ORDER BY id DESC limit 10 offset ${(page - 1) * 10}`;
        // console.log();
        return db.promisePool.query(sql)
    },

    // 请求网站名称和底部信息
    getmainmesage() {
        let sql = `SELECT * FROM db_website_set`;
        return db.promisePool.query(sql)

    },

    // 返回所有博客长度
    getblogitemnum() {
        // 请求到得数据一定要return回去
        let sql = `select count(*) blogitemlength from db_blog_item`;
        // console.log();
        return db.promisePool.query(sql)
    },

    // 根据id获取博客内容 和 评论 返回给前端
    getblogItemContent(id) {
        let sql = `select * from db_blog_item where content_id = '${id}'`;
        return db.promisePool.query(sql);

    },
    // 添加评论
    sendComment(commentId, contentId, userName, userImg, userEmail, leaveWord, createTimer) {
        // console.log('数据库', id, itemId, userName, userImg, userEmail, leaveWord, createTimer);
        let sql = `INSERT INTO db_comment_first(comment_id,content_id, user_name, user_img, user_email, leave_word,create_timer) VALUES ('${commentId}','${contentId}','${userName}','${userImg}','${userEmail}','${leaveWord}','${createTimer}')`;
        return db.promisePool.query(sql)
    },

    // 请求评论 和观看次数
    getcomment: (contentId) => {
        // 更新观看次数
        let addlook = `UPDATE db_blog_item SET look_count=look_count+1 WHERE content_id ='${contentId}'`;

        let sql = `SELECT * FROM db_comment_first WHERE content_id ='${contentId}'  ORDER BY id DESC`;
        db.promisePool.query(addlook)
        return db.promisePool.query(sql)
    },

    // 回复评论
    replycomment: (replyCommentId, commentId, userName, userImg, userEmail, replyWord, createTimer) => {
        let sql = `INSERT INTO db_reply_comment (comment_id, comment_first_id, user_name, user_img, user_email, reply_word, create_timer) VALUES ('${replyCommentId}','${commentId}','${userName}','${userImg}','${userEmail}','${replyWord}','${createTimer}')`;
        
        let updataReplyThere = `UPDATE db_comment_first SET there_reply = true WHERE comment_id ='${commentId}'`;
        db.promisePool.query(updataReplyThere)
        return db.promisePool.query(sql)
    },

    // 请求当前评论的所有回复
    searchreplylist: (commentID) => {
        let sql = `SELECT * FROM db_reply_comment WHERE comment_first_id = '${commentID}'`;
        return db.promisePool.query(sql)

    },
    //根据分类名称 请求对应的分类
    selectclass: (className) => {
        let sql = `SELECT * FROM db_blog_item WHERE article_class = '${className}'`;
        return db.promisePool.query(sql)
    },

    // 添加留言
    sendmessageboard: (id, messageText, createTimer) => {
        let sql = `INSERT INTO db_message_board(item_id, message_text, create_timer) VALUES ('${id}','${messageText}','${createTimer}')`
        return db.promisePool.query(sql)
    },

    // 请求所有留言
    getmessageboard: () => {
        // SELECT * FROM students ORDER BY score DESC
        let sql = `SELECT * FROM db_message_board ORDER BY id DESC`
        return db.promisePool.query(sql)
    }

}